#Requires -Version 3
#Requires -RunAsAdministrator

<#
.SYNOPSIS 
Hydration Script

.DESCRIPTION
Hydration Environment for MDT

.NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.

.LINK
http://MDTEx.CodePlex.com

#>

[CmdletBinding()]
param(
    [string] $MSIPackage,
    [switch] $test,
    [string] $ScriptVars = "$PSScriptRoot\ScriptBlock.xml"
)

#region Environment Setup

##########################################################

Write-Verbose "Starting $PSScriptRoot\FullTest.ps1"

$ErrorActionPreference  = "Stop" 
set-strictmode -version Latest

write-verbose "Load Library Modules $PSScriptRoot\Module\MDTEx"
Import-Module "$PSScriptRoot\..\Hydrate\Module\MDTEx" -force -ArgumentList ($PSBoundParameters['Verbose'] -eq $true) | out-string | write-verbose

set-LogPath "$env:temp\Hydrate.log"
"Start Hydration Full Test Regression" | Write-Verbose

$ScriptXML = Import-Clixml $ScriptVars
$scriptXML | out-string | Write-Verbose

#endregion

#region copy files to test machine

##########################################################

"Copy Scripts to Host for testing  $PSScriptRoot\HostTest  $($ScriptXML.HostShare)" | Write-Verbose
new-psdrive -Name TestTarget -PSProvider FileSystem -root $ScriptXML.HostShare -Credential $scriptxml.HostCredentials | out-string | Write-verbose
copy-item $PSScriptRoot\HostTest "$($ScriptXML.HostShare)\Staging" -Recurse -Force | out-string | Write-verbose
copy-item $PSScriptRoot\..\Hydrate\module "$($ScriptXML.HostShare)\Staging\module" -Recurse -force | out-string | Write-verbose
copy-item $MsiPackage "$($ScriptXML.HostShare)\Staging" -Force | out-string | Write-verbose

#endregion

#region run tests on remote machine

##########################################################

if ( $Test )
{
    "Invoke-Command -ScriptBlock {$($ScriptXML.HostCommand)} " | Write-verbose

    $RemoteParams = @{
        ComputerName = $ScriptXML.HostComputer
        Credential = $ScriptXML.HostCredentials
        AsJob = $true
        ScriptBlock = [Scriptblock]::Create($scriptxml.HostCommand)
    }

    $RemoteParams | Write-verbose
    $j = Invoke-Command @RemoteParams

    while (  $j.State -eq "Running" )
    {
        Receive-Job $j 
    }
}

#endregion

"End Hydration Full Regression" | Write-Verbose

